package com.leruan.aasimp.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.CarBeddingDao;
import com.leruan.aasimp.entity.CarBedding;

public class CarBeddingDaoImpl extends HibernateDaoSupport implements CarBeddingDao{

	public List<CarBedding> findAll() {
		Session session=this.getSession();
		String hql="from CarBedding";
		Query query=session.createQuery(hql);
		List<CarBedding> results=query.list();
		return results;
	}

	public void addLp(CarBedding carBedding) {
		Session session = this.getSession();
		session.save(carBedding);
	}

	public CarBedding findById(Long idBedding) {
		Session session=this.getSession();
		String hql="from CarBedding where idBedding=?";
		return (CarBedding) session.createQuery(hql).setLong(0, idBedding).uniqueResult();
	}

	public void update(CarBedding carBedding) {
		getHibernateTemplate().update(carBedding);
	}

	public void delete(CarBedding carBedding) {
		getHibernateTemplate().delete(carBedding);
	}

	public List<CarBedding> getByList(Map<String, String> map, int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarBedding where 1 = 1 ");
		if (map.get("idUserid") != null)
			sb.append(" and idUserid=:idUserid ");
		if (map.get("idCompany") != null)
			sb.append(" and idCompany=:idCompany ");
		Query query = session.createQuery(sb.toString());

		if (map.get("idUserid") != null)
			query.setString("idUserid", map.get("idUserid"));
		if (map.get("idCompany") != null)
			query.setLong("idCompany", Long.parseLong(map.get("idCompany")));
		
		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);
		List<CarBedding> results=query.list();
		return results;
	}

	public Long getTotal(Map<String, String> map) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select count(id) from CarBedding where 1 = 1 ");
		if (map.get("idUserid") != null)
			sb.append(" and idUserid=:idUserid ");
		if (map.get("idCompany") != null)
			sb.append(" and idCompany=:idCompany ");
		Query query = session.createQuery(sb.toString());

		if (map.get("idUserid") != null)
			query.setString("idUserid", map.get("idUserid"));
		if (map.get("idCompany") != null)
			query.setLong("idCompany", Long.parseLong(map.get("idCompany")));
		
		return (Long)query.uniqueResult();
	}

}
